import traceback

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async, try_except2

from apps.crawler_platform.core_abs.downstepabs import ParaItems
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.base_etl import BaseETL
from apps.crawler_platform.core_platform.core_etl import CoreLocalEtlControllerMixin
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from settings import get_settings


class BaseEtlItems(ParaItems):

    def __init__(self):
        super(BaseEtlItems, self).__init__()
        self.journal_rawid: str = ''
        self.etl_redis_journal_rawids = ''

    def set_downmodel(self):
        pass


class BaseETLStep(BaseETL, CoreLocalEtlControllerMixin):

    def __init__(self):
        super().__init__()

    async def my_task(self, table=""):
        """
        解析使用的my_task
        :return:
        """
        try:
            bools, err_info = await self.run_task()

            if not bools:
                bools_err, err_info_after = await self.err_collect(err_info, self.pm, self.batch, self.err_tag)
                if get_settings().GET_INFO_DEBUG == "true":
                    err_info["all_process_info"] = self.all_process_info
                if err_info:
                    err_info["process_info"] = self.process_info
                # 保底打标
                vv = self.statemanager.deal_my_task_failed(bools, err_info)
                if vv:
                    if vv["code"] == 0:
                        vv["code"] = 7
                    vv.update({"err_msg": str(err_info)})
                    where = self.pm.get_pkm()
                    table = self.pm.task_setting.table_name
                    await CoreSqlMixin.update_sql(table, vv, {}, where)
                return bools, err_info_after
            return bools, err_info

        except:
            # await CoreSqlMixin.insert_errinfo(self.pm.task_name, self.pm.task_tag, self.pm.Id, self.batch,
            #                                   str(traceback.format_exc()), self.err_tag)
            return False, {"traceback": traceback.format_exc()}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def run_task(self):
        bools, err_info = await self.task_step()
        return bools, err_info

    @try_except2()
    def verify_html(self):
        """
        验证html
        """
        return True, ""

    def deal_html(self, *args, **kwargs):
        """
        处理html
        :return:
        """
        return super().deal_html(*args, **kwargs)
